from pygame.sprite import Sprite, pygame, Group

class Buttons:
    unit_buttons = None
    action_buttons =  None
    turret_buttons = None
    
    def __init__(self ):
        self.unit_buttons = ButtonsGroup(["club_man","slinger","dino"],0)
        self.action_buttons = ButtonsGroup(["sell_turret","add_turret","evolution"],self.unit_buttons.get_width() + 40)
        self.turret_buttons = ButtonsGroup(["rock_slingshot", "egg_automatic", "primitive_catapult"], self.action_buttons.get_width() + 40)

    def draw(self, screen):
        self.unit_buttons.draw(screen)
        self.action_buttons.draw(screen)
        self.turret_buttons .draw(screen)

class ButtonsGroup(Group):
    position_x = 0
    
    def __init__(self, keys, starting_position ):
        Group.__init__(self)
        self.position_x = starting_position
        for i in keys:
            button = Button(i, (self.position_x,0))
            self.add( button )
            self.position_x += button.get_width()

    def get_width(self):
        return self.position_x

            

class Button(Sprite):
    key = None

    def __init__( self, key, pos ):
        Sprite.__init__(self)
        self.key = key
        file = "graphics/" + key + "_button.png"
        self.image = pygame.image.load(file)
        self.rect = self.image.get_rect()
        self.rect.topleft = pos

    def get_key(self):
        return self.key

    def get_width(self):
        return self.image.get_width()